<!DOCTYPE HTML>
<!--
	Dimension by HTML5 UP
	html5up.net | @ajlkn
	Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
 <head>
  <title>
   Dimension by HTML5 UP
  </title>
  <!-- <meta charset="utf-8" /> -->
  <!-- <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> -->
  <meta charset="utf-8"/>
  <meta content="width=device-width,initial-scale=1.0" name="viewport"/>
  <link href="../../assets/css/article.css" rel="stylesheet"/>
  <link href="https://cdn.bootcss.com/highlight.js/9.15.8/styles/github.min.css" rel="stylesheet"/>
  <noscript>
   <link href="../../assets/css/noscript.css" rel="stylesheet"/>
  </noscript>
 </head>
 <body>
  <div id="app">
  </div>
  <!-- built files will be auto injected -->
 </body>
 <body class="is-preload">
  <!-- Wrapper -->
  <div id="wrapper">
   <!-- Main -->
   <div id="main">
    <article id="article">
     <h1 id="spring-boot-websocket">
      Spring Boot WebSocket 使用记录
     </h1>
     <hr/>
     <p>
      这是我参与2022首次更文挑战的第28天，活动详情查看：
      <a href="https://juejin.cn/post/7052884569032392740">
       2022首次更文挑战
      </a>
     </p>
     <h2 id="_1">
      简介
     </h2>
     <p>
      使用spring boot 提供的websocket搭建websocket服务器，其中主要的有下面的三个部分：配置、通信处理、拦截器
     </p>
     <ul>
      <li>
       WebSocketConfigurer:websocket相关配置，主要配置其入口、消息处理类、拦截器
      </li>
      <li>
       WebSocketHandler：处理相关的通信，连接的建立、消息的发送、错误的处理，大部分的代码写在这里
      </li>
      <li>
       HandshakeInterceptor ：拦截器，可以使用默认的HttpSessionHandshakeInterceptor()即可
      </li>
     </ul>
     <h3 id="maven">
      Maven相关导入
     </h3>
     <div class="codehilite">
      <pre><span></span><code><span class="o">&lt;?</span><span class="n">xml</span> <span class="n">version</span><span class="o">=</span><span class="s">"1.0"</span> <span class="n">encoding</span><span class="o">=</span><span class="s">"UTF-8"</span><span class="o">?&gt;</span>
<span class="o">&lt;</span><span class="n">project</span> <span class="n">xmlns</span><span class="o">=</span><span class="s">"http://maven.apache.org/POM/4.0.0"</span> <span class="n">xmlns</span><span class="p">:</span><span class="n">xsi</span><span class="o">=</span><span class="s">"http://www.w3.org/2001/XMLSchema-instance"</span>
         <span class="n">xsi</span><span class="p">:</span><span class="n">schemaLocation</span><span class="o">=</span><span class="s">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">modelVersion</span><span class="o">&gt;</span><span class="mf">4.0.0</span><span class="o">&lt;/</span><span class="n">modelVersion</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">groupId</span><span class="o">&gt;</span><span class="n">com</span><span class="p">.</span><span class="na">example</span><span class="o">&lt;/</span><span class="n">groupId</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">artifactId</span><span class="o">&gt;</span><span class="n">visual</span><span class="o">&lt;/</span><span class="n">artifactId</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">version</span><span class="o">&gt;</span><span class="mf">0.0.1</span><span class="o">-</span><span class="n">SNAPSHOT</span><span class="o">&lt;/</span><span class="n">version</span><span class="o">&gt;</span>

    <span class="o">&lt;!--</span> <span class="n">Inherit</span> <span class="n">defaults</span> <span class="n">from</span> <span class="n">Spring</span> <span class="n">Boot</span> <span class="o">--&gt;</span>
    <span class="o">&lt;</span><span class="n">parent</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">groupId</span><span class="o">&gt;</span><span class="n">org</span><span class="p">.</span><span class="na">springframework</span><span class="p">.</span><span class="na">boot</span><span class="o">&lt;/</span><span class="n">groupId</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">artifactId</span><span class="o">&gt;</span><span class="n">spring</span><span class="o">-</span><span class="n">boot</span><span class="o">-</span><span class="n">starter</span><span class="o">-</span><span class="n">parent</span><span class="o">&lt;/</span><span class="n">artifactId</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">version</span><span class="o">&gt;</span><span class="mf">1.4.0</span><span class="p">.</span><span class="na">BUILD</span><span class="o">-</span><span class="n">SNAPSHOT</span><span class="o">&lt;/</span><span class="n">version</span><span class="o">&gt;</span>
    <span class="o">&lt;/</span><span class="n">parent</span><span class="o">&gt;</span>

    <span class="o">&lt;!--</span> <span class="n">Add</span> <span class="n">typical</span> <span class="n">dependencies</span> <span class="k">for</span> <span class="n">a</span> <span class="n">web</span> <span class="n">application</span> <span class="o">--&gt;</span>
    <span class="o">&lt;</span><span class="n">dependencies</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">dependency</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">groupId</span><span class="o">&gt;</span><span class="n">org</span><span class="p">.</span><span class="na">springframework</span><span class="p">.</span><span class="na">boot</span><span class="o">&lt;/</span><span class="n">groupId</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">artifactId</span><span class="o">&gt;</span><span class="n">spring</span><span class="o">-</span><span class="n">boot</span><span class="o">-</span><span class="n">starter</span><span class="o">-</span><span class="n">web</span><span class="o">&lt;/</span><span class="n">artifactId</span><span class="o">&gt;</span>
        <span class="o">&lt;/</span><span class="n">dependency</span><span class="o">&gt;</span>

        <span class="o">&lt;!--</span> <span class="n">websocket</span> <span class="o">--&gt;</span>
        <span class="o">&lt;</span><span class="n">dependency</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">groupId</span><span class="o">&gt;</span><span class="n">org</span><span class="p">.</span><span class="na">springframework</span><span class="p">.</span><span class="na">boot</span><span class="o">&lt;/</span><span class="n">groupId</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">artifactId</span><span class="o">&gt;</span><span class="n">spring</span><span class="o">-</span><span class="n">boot</span><span class="o">-</span><span class="n">starter</span><span class="o">-</span><span class="n">websocket</span><span class="o">&lt;/</span><span class="n">artifactId</span><span class="o">&gt;</span>
        <span class="o">&lt;/</span><span class="n">dependency</span><span class="o">&gt;</span>

        <span class="o">&lt;!--</span> <span class="n">用于测试的依赖项</span> <span class="o">--&gt;</span>
        <span class="o">&lt;</span><span class="n">dependency</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">groupId</span><span class="o">&gt;</span><span class="n">org</span><span class="p">.</span><span class="na">mockito</span><span class="o">&lt;/</span><span class="n">groupId</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">artifactId</span><span class="o">&gt;</span><span class="n">mockito</span><span class="o">-</span><span class="n">core</span><span class="o">&lt;/</span><span class="n">artifactId</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">version</span><span class="o">&gt;</span><span class="mf">1.9.5</span><span class="o">&lt;/</span><span class="n">version</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">scope</span><span class="o">&gt;</span><span class="n">test</span><span class="o">&lt;/</span><span class="n">scope</span><span class="o">&gt;</span>
        <span class="o">&lt;/</span><span class="n">dependency</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">dependency</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">groupId</span><span class="o">&gt;</span><span class="n">junit</span><span class="o">&lt;/</span><span class="n">groupId</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">artifactId</span><span class="o">&gt;</span><span class="n">junit</span><span class="o">&lt;/</span><span class="n">artifactId</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">scope</span><span class="o">&gt;</span><span class="n">test</span><span class="o">&lt;/</span><span class="n">scope</span><span class="o">&gt;</span>
        <span class="o">&lt;/</span><span class="n">dependency</span><span class="o">&gt;</span>
    <span class="o">&lt;/</span><span class="n">dependencies</span><span class="o">&gt;</span>

    <span class="o">&lt;!--</span> <span class="n">Package</span> <span class="n">as</span> <span class="n">an</span> <span class="n">executable</span> <span class="n">jar</span> <span class="o">--&gt;</span>
    <span class="o">&lt;</span><span class="n">build</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">plugins</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">plugin</span><span class="o">&gt;</span>
                <span class="o">&lt;</span><span class="n">groupId</span><span class="o">&gt;</span><span class="n">org</span><span class="p">.</span><span class="na">springframework</span><span class="p">.</span><span class="na">boot</span><span class="o">&lt;/</span><span class="n">groupId</span><span class="o">&gt;</span>
                <span class="o">&lt;</span><span class="n">artifactId</span><span class="o">&gt;</span><span class="n">spring</span><span class="o">-</span><span class="n">boot</span><span class="o">-</span><span class="n">maven</span><span class="o">-</span><span class="n">plugin</span><span class="o">&lt;/</span><span class="n">artifactId</span><span class="o">&gt;</span>
            <span class="o">&lt;/</span><span class="n">plugin</span><span class="o">&gt;</span>
        <span class="o">&lt;/</span><span class="n">plugins</span><span class="o">&gt;</span>
    <span class="o">&lt;/</span><span class="n">build</span><span class="o">&gt;</span>

    <span class="o">&lt;!--</span> <span class="n">Add</span> <span class="n">Spring</span> <span class="n">repositories</span> <span class="o">--&gt;</span>
    <span class="o">&lt;!--</span> <span class="p">(</span><span class="n">you</span> <span class="n">don</span><span class="err">'</span><span class="n">t</span> <span class="n">need</span> <span class="k">this</span> <span class="k">if</span> <span class="n">you</span> <span class="n">are</span> <span class="n">using</span> <span class="n">a</span> <span class="p">.</span><span class="na">RELEASE</span> <span class="n">versio</span>
    <span class="n">n</span><span class="p">)</span> <span class="o">--&gt;</span>
    <span class="o">&lt;</span><span class="n">repositories</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">repository</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">id</span><span class="o">&gt;</span><span class="n">spring</span><span class="o">-</span><span class="n">snapshots</span><span class="o">&lt;/</span><span class="n">id</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">url</span><span class="o">&gt;</span><span class="n">http</span><span class="p">:</span><span class="c1">//repo.spring.io/snapshot&lt;/url&gt;</span>
            <span class="o">&lt;</span><span class="n">snapshots</span><span class="o">&gt;&lt;</span><span class="n">enabled</span><span class="o">&gt;</span><span class="kc">true</span><span class="o">&lt;/</span><span class="n">enabled</span><span class="o">&gt;&lt;/</span><span class="n">snapshots</span><span class="o">&gt;</span>
        <span class="o">&lt;/</span><span class="n">repository</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">repository</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">id</span><span class="o">&gt;</span><span class="n">spring</span><span class="o">-</span><span class="n">milestones</span><span class="o">&lt;/</span><span class="n">id</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">url</span><span class="o">&gt;</span><span class="n">http</span><span class="p">:</span><span class="c1">//repo.spring.io/milestone&lt;/url&gt;</span>
        <span class="o">&lt;/</span><span class="n">repository</span><span class="o">&gt;</span>
    <span class="o">&lt;/</span><span class="n">repositories</span><span class="o">&gt;</span>
    <span class="o">&lt;</span><span class="n">pluginRepositories</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">pluginRepository</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">id</span><span class="o">&gt;</span><span class="n">spring</span><span class="o">-</span><span class="n">snapshots</span><span class="o">&lt;/</span><span class="n">id</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">url</span><span class="o">&gt;</span><span class="n">http</span><span class="p">:</span><span class="c1">//repo.spring.io/snapshot&lt;/url&gt;</span>
        <span class="o">&lt;/</span><span class="n">pluginRepository</span><span class="o">&gt;</span>
        <span class="o">&lt;</span><span class="n">pluginRepository</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">id</span><span class="o">&gt;</span><span class="n">spring</span><span class="o">-</span><span class="n">milestones</span><span class="o">&lt;/</span><span class="n">id</span><span class="o">&gt;</span>
            <span class="o">&lt;</span><span class="n">url</span><span class="o">&gt;</span><span class="n">http</span><span class="p">:</span><span class="c1">//repo.spring.io/milestone&lt;/url&gt;</span>
        <span class="o">&lt;/</span><span class="n">pluginRepository</span><span class="o">&gt;</span>
    <span class="o">&lt;/</span><span class="n">pluginRepositories</span><span class="o">&gt;</span>
<span class="o">&lt;/</span><span class="n">project</span><span class="o">&gt;</span>
</code></pre>
     </div>
     <h3 id="_2">
      主类编写
     </h3>
     <div class="codehilite">
      <pre><span></span><code><span class="kn">import</span> <span class="nn">org.springframework.boot.SpringApplication</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.boot.autoconfigure.SpringBootApplication</span><span class="p">;</span>

<span class="c1">// SpringBootApplication 这个注入表示整个工程的入口</span>
<span class="nd">@SpringBootApplication</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Main</span> <span class="p">{</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="p">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="p">)</span> <span class="p">{</span>
        <span class="c1">// 设置服务的监听端口</span>
        <span class="n">System</span><span class="p">.</span><span class="na">getProperties</span><span class="p">().</span><span class="na">put</span><span class="p">(</span> <span class="s">"server.port"</span><span class="p">,</span> <span class="mi">8000</span><span class="p">);</span>
        <span class="n">SpringApplication</span><span class="p">.</span><span class="na">run</span><span class="p">(</span><span class="n">Main</span><span class="p">.</span><span class="na">class</span><span class="p">,</span> <span class="n">args</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
     </div>
     <h3 id="websockethandler">
      WebSocketHandler 消息处理类编写
     </h3>
     <div class="codehilite">
      <pre><span></span><code><span class="kn">import</span> <span class="nn">org.springframework.web.socket.*</span><span class="p">;</span>

<span class="kn">import</span> <span class="nn">java.io.IOException</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.util.HashMap</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">java.util.Map</span><span class="p">;</span>

<span class="cm">/**</span>
<span class="cm"> * websocket的处理程序</span>
<span class="cm"> */</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">SafetyAttackHandler</span> <span class="kd">implements</span> <span class="n">WebSocketHandler</span><span class="p">{</span>
    <span class="c1">// 用于保存所有连接的客户端信息</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="n">Map</span><span class="o">&lt;</span><span class="n">WebSocketSession</span><span class="p">,</span> <span class="n">Map</span><span class="o">&gt;</span> <span class="n">clients</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HashMap</span><span class="o">&lt;</span><span class="n">WebSocketSession</span><span class="p">,</span> <span class="n">Map</span><span class="o">&gt;</span><span class="p">();</span>

    <span class="cm">/**</span>
<span class="cm">     * 连接成功后的操作</span>
<span class="cm">     * 连接成功建立以后添加客户端信息</span>
<span class="cm">     * @param webSocketSession</span>
<span class="cm">     * @throws Exception</span>
<span class="cm">     */</span>
    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">afterConnectionEstablished</span><span class="p">(</span><span class="n">WebSocketSession</span> <span class="n">webSocketSession</span><span class="p">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="p">{</span>
        <span class="n">webSocketSession</span><span class="p">.</span><span class="na">sendMessage</span><span class="p">(</span><span class="k">new</span> <span class="n">TextMessage</span><span class="p">(</span><span class="s">"Connect successful"</span><span class="p">));</span>
        <span class="n">clients</span><span class="p">.</span><span class="na">put</span><span class="p">(</span><span class="n">webSocketSession</span><span class="p">,</span> <span class="k">new</span> <span class="n">HashMap</span><span class="p">());</span>
    <span class="p">}</span>

    <span class="cm">/**</span>
<span class="cm">     * 收到消息后的处理</span>
<span class="cm">     * @param webSocketSession</span>
<span class="cm">     * @param webSocketMessage</span>
<span class="cm">     * @throws Exception</span>
<span class="cm">     */</span>
    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">handleMessage</span><span class="p">(</span><span class="n">WebSocketSession</span> <span class="n">webSocketSession</span><span class="p">,</span> <span class="n">WebSocketMessage</span><span class="o">&lt;?&gt;</span> <span class="n">webSocketMessage</span><span class="p">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="p">{</span>

    <span class="p">}</span>

    <span class="cm">/**</span>
<span class="cm">     * 发生错误后的处理</span>
<span class="cm">     * 移除相应的客户端信息</span>
<span class="cm">     * @param webSocketSession</span>
<span class="cm">     * @param throwable</span>
<span class="cm">     * @throws Exception</span>
<span class="cm">     */</span>
    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">handleTransportError</span><span class="p">(</span><span class="n">WebSocketSession</span> <span class="n">webSocketSession</span><span class="p">,</span> <span class="n">Throwable</span> <span class="n">throwable</span><span class="p">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="p">{</span>
        <span class="n">clients</span><span class="p">.</span><span class="na">remove</span><span class="p">(</span><span class="n">webSocketSession</span><span class="p">);</span>
        <span class="n">webSocketSession</span><span class="p">.</span><span class="na">sendMessage</span><span class="p">(</span><span class="k">new</span> <span class="n">TextMessage</span><span class="p">(</span><span class="n">throwable</span><span class="p">.</span><span class="na">toString</span><span class="p">()));</span>
    <span class="p">}</span>

    <span class="cm">/**</span>
<span class="cm">     * 连接关闭时的处理</span>
<span class="cm">     * 移除关闭的客户端</span>
<span class="cm">     * @param webSocketSession</span>
<span class="cm">     * @param closeStatus</span>
<span class="cm">     * @throws Exception</span>
<span class="cm">     */</span>
    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">afterConnectionClosed</span><span class="p">(</span><span class="n">WebSocketSession</span> <span class="n">webSocketSession</span><span class="p">,</span> <span class="n">CloseStatus</span> <span class="n">closeStatus</span><span class="p">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="p">{</span>
        <span class="n">clients</span><span class="p">.</span><span class="na">remove</span><span class="p">(</span><span class="n">webSocketSession</span><span class="p">);</span>
        <span class="n">webSocketSession</span><span class="p">.</span><span class="na">sendMessage</span><span class="p">(</span><span class="k">new</span> <span class="n">TextMessage</span><span class="p">(</span><span class="s">"Close connect"</span><span class="p">));</span>
    <span class="p">}</span>

    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">supportsPartialMessages</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="cm">/**</span>
<span class="cm">     * 推送消息到所有的客户端</span>
<span class="cm">     * @param message</span>
<span class="cm">     */</span>
    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">sendMessageToAll</span><span class="p">(</span><span class="n">String</span> <span class="n">message</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">for</span><span class="p">(</span><span class="n">WebSocketSession</span> <span class="n">session</span> <span class="p">:</span> <span class="n">clients</span><span class="p">.</span><span class="na">keySet</span><span class="p">())</span> <span class="p">{</span>
            <span class="k">try</span> <span class="p">{</span>
                <span class="n">session</span><span class="p">.</span><span class="na">sendMessage</span><span class="p">(</span><span class="k">new</span> <span class="n">TextMessage</span><span class="p">(</span><span class="n">message</span><span class="p">));</span>
            <span class="p">}</span> <span class="k">catch</span> <span class="p">(</span><span class="n">IOException</span> <span class="n">e</span><span class="p">)</span> <span class="p">{</span>
                <span class="n">e</span><span class="p">.</span><span class="na">printStackTrace</span><span class="p">();</span>
            <span class="p">}</span>
        <span class="p">}</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
     </div>
     <h3 id="websocketconfigurer">
      WebSocketConfigurer 配置编写
     </h3>
     <div class="codehilite">
      <pre><span></span><code><span class="kn">import</span> <span class="nn">cn.nssas.eelantech.handlers.SafetyAttackHandler</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.context.annotation.Bean</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.context.annotation.Configuration</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.web.socket.WebSocketHandler</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.web.socket.config.annotation.EnableWebSocket</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.web.socket.config.annotation.WebSocketConfigurer</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry</span><span class="p">;</span>
<span class="kn">import</span> <span class="nn">org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor</span><span class="p">;</span>

<span class="cm">/**</span>
<span class="cm"> * websocket接口配置</span>
<span class="cm"> */</span>
<span class="nd">@Configuration</span>
<span class="nd">@EnableWebSocket</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">SafetyAttackWebSocket</span> <span class="kd">implements</span> <span class="n">WebSocketConfigurer</span> <span class="p">{</span>

    <span class="cm">/**</span>
<span class="cm">     * 设置连接的路由、处理相关类</span>
<span class="cm">     * addHandler：设置websocket的处理类和相应接口URL："/safetyAttackWebsocket"</span>
<span class="cm">     * setAllowedOrigins：设置链接允许，*代码允许所有情况，需要进行此设置，不然可能导致连接失败</span>
<span class="cm">     * @param webSocketHandlerRegistry</span>
<span class="cm">     */</span>
    <span class="nd">@Override</span>
    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">registerWebSocketHandlers</span><span class="p">(</span><span class="n">WebSocketHandlerRegistry</span> <span class="n">webSocketHandlerRegistry</span><span class="p">)</span> <span class="p">{</span>
        <span class="n">webSocketHandlerRegistry</span><span class="p">.</span><span class="na">addHandler</span><span class="p">(</span><span class="n">myHandler</span><span class="p">(),</span> <span class="s">"/safetyAttackWebsocket"</span><span class="p">).</span><span class="na">addInterceptors</span><span class="p">(</span><span class="k">new</span> <span class="n">HttpSessionHandshakeInterceptor</span><span class="p">()).</span><span class="na">setAllowedOrigins</span><span class="p">(</span><span class="s">"*"</span><span class="p">);</span>
    <span class="p">}</span>

    <span class="cm">/**</span>
<span class="cm">     * 返回websocket连接对应处理类</span>
<span class="cm">     * @return</span>
<span class="cm">     */</span>
    <span class="nd">@Bean</span>
    <span class="kd">public</span> <span class="n">WebSocketHandler</span> <span class="nf">myHandler</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">return</span> <span class="k">new</span> <span class="n">SafetyAttackHandler</span><span class="p">();</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
     </div>
     <h3 id="_3">
      测试连接
     </h3>
     <ul>
      <li>
       <a href="http://www.blue-zero.com/WebSocket/">
        可以使用页面的测试
       </a>
      </li>
      <li>
       输入：ws://127.0.0.1:8000/safetyAttackWebsocke，进行连接即可
      </li>
     </ul>
     <h2 id="_4">
      参考链接
     </h2>
     <ul>
      <li>
       <a href="https://blog.csdn.net/u014520745/article/details/62046396">
        spring配置websocket并实现群发/单独发送消息
       </a>
      </li>
      <li>
       <a href="https://segmentfault.com/a/1190000014862001">
        springboot整合websocket实现一对一消息推送和广播消息推送
       </a>
      </li>
      <li>
       <a href="https://blog.csdn.net/u014042066/article/details/76702120">
        SpringBoot-WebSocket广播消息+单点消息(指定用户发送消息)
       </a>
      </li>
      <li>
       <a href="https://docs.spring.io/spring/docs/4.3.3.RELEASE/spring-framework-reference/htmlsingle/#websocket">
        WebSocket API
       </a>
      </li>
     </ul>
    </article>
   </div>
   <!-- Footer -->
   <footer id="footer">
    <p class="copyright">
     © Untitled. Design:
     <a href="https://html5up.net">
      HTML5 UP
     </a>
     .
    </p>
   </footer>
  </div>
  <!-- BG -->
  <div id="bg">
  </div>
  <!-- Scripts -->
  <script src="../assets/js/jquery.min.js">
  </script>
  <script src="../assets/js/browser.min.js">
  </script>
  <script src="../assets/js/breakpoints.min.js">
  </script>
  <script src="../assets/js/util.js">
  </script>
  <script src="../assets/js/main.js">
  </script>
 </body>
</html>
